<!--
 * @Descripttion: 交易设置
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2020-09-28 15:24:24
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-08-15 17:50:08
-->
<template>
  <div class="lb-system-transaction">
    <top-nav />
    <div class="page-main">
      <lb-tips>
        系统默认用户订单处于待核销状态或完成订单后24小时内可申请退款
      </lb-tips>
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="140px"
      >
        <el-form-item label="提现方式" prop="checkList">
          <el-checkbox-group v-model="checkList" @change="changeCheckBox">
            <div
              v-for="(item, index) in authList"
              :key="index"
              :style="{
                display: 'inline-block',
                marginLeft: index === 0 ? 0 : '15px'
              }"
              v-show="
                item.key !== 'bank_transfer' ||
                (item.key === 'bank_transfer' && routesItem.auth.adapay)
              "
            >
              <el-checkbox :label="item.title"></el-checkbox>
              <lb-tool-tips v-if="item.tips">{{ item.tips }}</lb-tool-tips>
            </div>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="转账方式" prop="company_pay">
          <el-radio-group v-model="subForm.company_pay">
            <el-radio :label="1">企业转账</el-radio>
            <el-radio :label="2">商家转账</el-radio>
          </el-radio-group>
          <lb-tool-tips>用于微信线上提现</lb-tool-tips>
        </el-form-item>
        <el-form-item label="车费秒到账" prop="car_price_account">
          <el-radio-group v-model="subForm.car_price_account">
            <el-radio :label="1">{{ $t('action.ON') }}</el-radio>
            <el-radio :label="0">{{ $t('action.OFF') }}</el-radio>
          </el-radio-group>
          <lb-tool-tips
            >开启之后，车费提成将会在技师点击已到达后，立马转入到技师的微信零钱包
            <div class="mt-sm">
              不具备商家转账功能的企业不能使用该功能
            </div></lb-tool-tips
          >
        </el-form-item>
        <el-form-item
          label="秒到账支付方式"
          prop="account_pay_type"
          v-if="subForm.car_price_account === 1"
        >
          <el-radio-group v-model="subForm.account_pay_type">
            <el-radio :label="1">微信</el-radio>
            <el-radio :label="2">支付宝</el-radio>
          </el-radio-group>
          <lb-tool-tips
            >若选择支付宝，但{{
              $t('action.attendantName')
            }}未填写支付宝账号信息，将无法自动到账</lb-tool-tips
          >
        </el-form-item>

        <el-form-item label="最低提现额度" prop="cash_mini">
          <el-input
            v-model="subForm.cash_mini"
            placeholder="请输入最低提现额度"
          >
            <template slot="append">元</template></el-input
          >
          <lb-tool-tips
            >当{{
              $t('action.attendantName')
            }}、分销合伙人、渠道商、代理商、业务员申请提现时，限制提现的最低额度</lb-tool-tips
          >
        </el-form-item>
        <el-form-item label="扣税百分点" prop="tax_point">
          <el-input v-model="subForm.tax_point" placeholder="请输入扣税百分点">
            <template slot="append">%</template>
          </el-input>
          <lb-tool-tips
            >设置扣税百分比之后，当{{
              $t('action.attendantName')
            }}、分销合伙人、渠道商、代理商、业务员申请提现时会产生税费
            <div class="mt-sm">
              例如：申请提现金额100元，扣税比例为3%，则税费=100*3%=3元，实际提现应到账金额=100-3=97元
            </div>
          </lb-tool-tips>
        </el-form-item>
        <el-form-item label="交易规则" prop="trading_rules">
          <lb-ueditor
            v-model="subForm.trading_rules"
            :destroy="true"
            :ueditorType="3"
          ></lb-ueditor>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo" v-preventReClick>{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
export default {
  data () {
    let checkList = (rule, value, callback) => {
      let { wechat_transfer: wechat = 0, alipay_transfer: alipay = 0, under_transfer: under } = this.subForm
      if (!wechat && !alipay && !under) {
        callback(new Error(`请选择提现方式`))
      } else {
        callback()
      }
    }
    return {
      checkList: [],
      authList: [{ title: '微信提现', key: 'wechat_transfer' }, { title: '支付宝提现', key: 'alipay_transfer' }, { title: '线下提现', key: 'under_transfer', tips: '用于用户、' + this.$t('action.attendantName') + '或代理商提现时选择到账方式' }],
      subForm: {
        wechat_transfer: 0,
        alipay_transfer: 0,
        under_transfer: 0,
        company_pay: 1,
        car_price_account: 0,
        account_pay_type: 1,
        cash_mini: '',
        tax_point: '',
        trading_rules: ''
      },
      subFormRules: {
        checkList: { required: true, validator: checkList, trigger: ['blur', 'change'] },
        company_pay: { required: true, type: 'number', message: '请选择转账方式', trigger: 'blur' },
        car_price_account: { required: true, type: 'number', message: '请选择车费秒到账', trigger: 'blur' },
        account_pay_type: { required: true, type: 'number', message: '请选择秒到账支付方式', trigger: 'blur' },
        cash_mini: { required: true, validator: this.$reg.isMoney, trigger: 'blur' },
        tax_point: { required: true, validator: this.$reg.isFloatNum, text: '扣税百分点', reg_type: 2, max: 100, trigger: ['blur', 'change'] }
      }
    }
  },
  created () {
    this.getFormInfo()
  },
  computed: {
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    async getFormInfo () {
      let { code, data } = await this.$api.system.configInfo()
      if (code !== 200) return
      for (let i in this.subForm) {
        this.subForm[i] = data[i]
      }
      let checkItem = []
      this.authList.map(item => {
        if (data[item.key] === 1) {
          checkItem.push(item.title)
        }
      })
      this.checkList = checkItem
    },
    changeCheckBox (e) {
      this.authList.map(item => {
        this.subForm[item.key] = e.includes(item.title) ? 1 : 0
      })
    },
    async submitFormInfo () {
      let flag = false
      this.$refs['subForm'].validate(valid => {
        if (valid) {
          flag = true
        }
      })
      let subForm = JSON.parse(JSON.stringify(this.subForm))
      if (flag) {
        await this.$api.system.configUpdate(subForm)
        this.$message.success(this.$t('tips.successSub'))
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-system-transaction {
  width: 100%;
  .page-main {
    padding: 20px;
    .el-form {
      width: 100%;
      .el-form-item {
        margin-bottom: 24px;
        .el-select,
        .el-input-number,
        .el-input {
          width: 300px;
        }
      }
      .last-form-item {
        margin-top: 30px;
      }
      .item-tips {
        margin-left: 120px;
        margin-bottom: 24px;
        color: #999999;
      }
    }
  }
}
</style>
